<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="hAdd"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="roleList">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template slot-scope="{row}">
                  <el-button size="small" type="success" @click="hAssignPermission(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="hEdit(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="hDel(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                background
                :current-page="p.page"
                :page-sizes="[2, 3, 5, 10]"
                :page-size="p.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 新增弹框 -->
      <el-dialog
        :title="isEdit ?'编辑角色' :'添加角色'"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="showDialog"
        @closed="close"
      >
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="showDialog=false">取消</el-button>
            <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
      <!-- 分配权限的弹层 -->
      <el-dialog
        title="分配权限(一级为路由页面查看权限-二级为按钮操作权限)"
        :visible.sync="showDialogAssign"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
      >
        <assign-permission
          v-if="showDialogAssign"
          :id="id"
          ref="assignRef"
          @close="showDialogAssign=false"
          @success="hSuccess"
        />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getSetRoleList, DelSetRoleList, addRole, getRoleInfo, updateRoleInfo } from '@/api/settings'
import AssignPermission from './assignPermission.vue'
export default {
  components: {
    AssignPermission
  },
  data() {
    return {
      showDialogAssign: false, // 分配权限的弹层
      id: '',
      roleList: [],
      p: {
        page: 1,
        pagesize: 2
      },
      total: 0,
      showDialog: false,
      isEdit: false,
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '角色描述不能为空', trigger: 'blur' }

        ]
      }
    }
  },

  created() {
    this.loadRoleList()
  },
  methods: {
    hSuccess() {
      this.showDialogAssign = false
      this.loadRoleList()
    },
    // 分配权限
    hAssignPermission(id) {
      this.showDialogAssign = true
      this.id = id
    },
    // 重置表单
    close() {
      this.roleForm = {
        name: '',
        description: ''
      }
      this.$refs.roleForm.resetFields()
    },
    // 编辑
    async doEdit() {
      try {
        const res = await updateRoleInfo(this.roleForm)
        console.log(res)
        this.$message.success(res.message)
        this.showDialog = false
        this.loadRoleList()
      } catch (err) {
        console.log(err)
      }
    },
    // 根据ID获取角色详情回填数据
    async hEdit(id) {
      this.isEdit = true
      this.showDialog = true
      try {
        const res = await getRoleInfo(id)
        console.log(res)
        this.roleForm = res.data
      } catch (err) {
        console.log(err)
      }
    },
    // 添加
    async doAdd() {
      try {
        const res = await addRole(this.roleForm)
        console.log(res)
        this.$message.success(res.message)
        this.showDialog = false
        // 将当前页码值改为最后一页的值
        this.total++
        this.p.page = Math.ceil(this.total / this.p.pagesize)
        this.loadRoleList()
      } catch (err) {
        console.log(err)
      }
    },
    hAdd() {
      this.showDialog = true
      this.isEdit = false
    },
    // 弹框确认按钮
    hSubmit() {
      // 兜底校验
      this.$refs.roleForm.validate(async valid => {
        if (!valid) return
        this.isEdit ? this.doEdit() : this.doAdd()
      })
    },

    // 删除
    async doDel(id) {
      try {
        const res = await DelSetRoleList(id)
        console.log(res)
        if (this.roleList.length === 1) {
          this.p.page--
          if (this.p.page <= 1) {
            this.p.page = 1
          }
        }
        if (res.success) {
          this.$message.success(res.message)
          this.loadRoleList()
        }
      } catch (err) {
        console.log(err)
      }
    },
    // 删除角色对话框
    hDel(id) {
      this.$confirm('确认删除该角色吗, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 确定
        this.doDel(id)
      }).catch(() => {
        // 取消
      })
    },
    // 加载所有角色列表
    async loadRoleList() {
      try {
        const res = await getSetRoleList(this.p)
        console.log(res)
        this.roleList = res.data.rows
        this.total = res.data.total
      } catch (err) {
        console.log(err)
      }
    },
    // 控制每页显示的条数
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.p.pagesize = val
      this.p.page = 1
      this.loadRoleList()
    },
    // 控制页码值变化
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.p.page = val
      this.loadRoleList()
    }
  }
}
</script>
